package com.gjl.app.view.web.action;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.gjl.app.model.business.blog.entity.BlogCategoryEntity;
import com.gjl.app.model.business.blog.entity.BlogCommentEntity;
import com.gjl.app.model.business.blog.entity.BlogEntity;
import com.gjl.app.model.business.entity.GuestBookItem;
import com.gjl.app.view.web.BaseAction;
import com.google.appengine.api.datastore.Text;

public class BlogAction extends BaseAction {

	private static final long serialVersionUID = 6393512035693604584L;
	
	private Long blogCategory;
	
	private String blogContent;
	
	private String title;
	
	private String id;

	public Long getBlogCategory() {
		return blogCategory;
	}

	public void setBlogCategory(Long blogCategory) {
		this.blogCategory = blogCategory;
	}

	public String getBlogContent() {
		return blogContent;
	}

	public void setBlogContent(String blogContent) {
		this.blogContent = blogContent;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String listBlog() throws Exception {
		EntityManager entityManager = getEntityManager();
		String blogQuery = "SELECT blog FROM " + BlogEntity.class.getName() +" blog";
		Query query = entityManager.createQuery(blogQuery);
		List<BlogEntity> blog = (List<BlogEntity>)query.getResultList();
		if (blog != null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("blog", blog);
		}
		return SUCCESS;
	}
	
	public String newBlog() throws Exception {
		EntityManager entityManager = getEntityManager();
		String cateQuery = "SELECT cate FROM " + BlogCategoryEntity.class.getName() +" cate";
		Query query = entityManager.createQuery(cateQuery);
		List<BlogCategoryEntity> category = (List<BlogCategoryEntity>)query.getResultList();
		if (category != null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("category", category);
		}
		return SUCCESS;
	}
	
	public String postBlog() throws Exception {
		BlogEntity blog = new BlogEntity();
		blog.setContent(new Text(blogContent));
		blog.setCategoryId(blogCategory);
		blog.setTitle(title);
		EntityManager  entityManager = getEntityManager();
		EntityTransaction utx = entityManager.getTransaction();
		utx.begin();
		entityManager.persist(blog);
		entityManager.flush();
		utx.commit();
		return SUCCESS;
	}
	
	public String viewBlog() throws Exception {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String idValue = request.getParameter("id");
			if (idValue != null) {
				Long blogId = Long.valueOf(idValue);
				EntityManager entityManager = getEntityManager();
				Query blogQuery = entityManager.createQuery("SELECT item FROM "+BlogEntity.class.getCanonicalName()+" item where item.id = ?1");
				blogQuery.setParameter(1, blogId);
				BlogEntity blog = (BlogEntity)blogQuery.getSingleResult();
				if (blog != null) {
					request.setAttribute("blog", blog);
				}
			}
		}catch(Exception e) {
			
		}
		return SUCCESS;
	}
	
	public String commentBlog() throws Exception {
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			BlogCommentEntity blogComment = new BlogCommentEntity(blogContent);
			id = request.getParameter("id");
			Long blogId = null;
			blogId = Long.valueOf(id);
			EntityManager entityManager = getEntityManager();
			EntityTransaction utx = entityManager.getTransaction();
			utx.begin();
			BlogEntity blog = (BlogEntity)entityManager.find(BlogEntity.class, blogId);
			blogComment.setBlog(blog);
			entityManager.persist(blogComment);
			entityManager.flush();
			utx.commit();
		}catch(Exception e) {
			e.printStackTrace();
		}
			return SUCCESS;
	}

}
